<?php
	require_once("alert.php");
	require_once("base_mod.php");

	class AlertMod extends AlertBaseMod {
		
		public function getThreats(){
			$threats = "";
			$conn = self::getConnection();
			$sql = "SELECT * FROM alerts a where a.cleared = 0 and a.superceded = 0 order by a.sector";
			$rows = mysql_query($sql, $conn);
			$currentSector = "";
			$alertCount = 0;
			$sectorThreatCount = 0;
			while ($row = mysql_fetch_assoc($rows)) {
				$sector = $row["sector"];
				$coords = trim($row["coords"], "[]");
				if ($sector != $currentSector){
					$currentSector = $sector;
					if ($alertCount > 0) {
						$threats = rtrim($threats, ";");
						$threats = $threats.";".$sectorThreatCount."|".$currentSector.";".$row["attacker"].":".$coords.";";
					}else{
						$threats = $currentSector.";".$row["attacker"].":".$coords.";";
					}
					$sectorThreatCount = 1;
				}else{
					$threats = $threats.$row["attacker"].":".$coords.";";
					$sectorThreatCount++;
				}
				$alertCount++;
			}
			return $threats.$sectorThreatCount;
		}
		
		public function clearTile($remtile){
			if ($remtile != ""){
				$data = explode(";", $remtile);
				$sector = $data[0];
				$soords = "[".$data[1]."]";
				$conn = self::getConnection();
				$sql = sprintf(
					"update alerts set cleared = 1 where sector = '%s' and coords = '%s'", 
					mysql_real_escape_string($sector),
					mysql_real_escape_string($soords)
					);
				//echo $sql;
				mysql_query($sql, $conn);
			}
		}
	
		public function getAlerts(){
			$alerts = self::getAllAlerts();
			$response = "";
			foreach($alerts as $alt):
				$response = $response .$alt->getCluster()."|".$alt->getSector()."|".$alt->getCoords()."|".$alt->getReason()."|".$alt->getOffender()."|".$alt->getName().";";
			endforeach;
			return $response;
		}
		
		private function getAllAlerts() {
			$alerts = array();
			$conn = self::getConnection();
			$sql = "SELECT * FROM alerts a where a.cleared = 0 and a.superceded = 0 ORDER BY a.when desc";
			//echo $sql;
			$rows = mysql_query($sql, $conn); 
			while ($row = mysql_fetch_assoc($rows)) {
				$clustr = $row["cluster"];
				$rsn = $row["additional"];
				if ($rsn != "Ambushed") $rsn = "PvP";
				if ($row["type"] == "Building attack") $rsn = "Building attack";
				if (is_null($clustr)) $clustr = "";
				$alert =
					new Alert(
						$row["when"],
						$clustr,
						$row["sector"],
						$row["coords"],
						$row["type"],
						$row["attacker"],
						$row["defender"],
						$row["alert_type"],
						$row["id"],
						$row["cleared"]
					);
			//echo $alert->getSector();
			$alerts[] = $alert;
			}
			//echo count("     ".$alerts);
			return $alerts;
			
		}
		
		public function getAlertsForList($cluster) {
			$alerts = array();
			$conn = self::getConnection();
			if (!empty($cluster))
				$sql = "SELECT * FROM alerts a where a.cluster = '".$cluster."' ORDER BY a.when desc";
			else
				$sql = "SELECT * FROM alerts a ORDER BY a.when desc";
			//echo $sql;
			$rows = mysql_query($sql, $conn); 
			while ($row = mysql_fetch_assoc($rows)) {
				$clustr = $row["cluster"];
				$rsn = $row["additional"];
				if ($rsn != "Ambushed") $rsn = "PvP";
				if ($row["type"] == "Building attack") $rsn = "Building attack";
				if (is_null($clustr)) $clustr = "";
				$alert =
					new Alert(
						$row["when"],
						$clustr,
						$row["sector"],
						$row["coords"],
						$row["type"],
						$row["attacker"],
						$row["defender"],
						$row["alert_type"],
						$row["pid"],
						$row["cleared"]
					);
			//echo $alert->getSector();
			$alerts[] = $alert;
			}
			//echo count("     ".$alerts);
			return $alerts;
			
		}
		
		public function getManualAlertById($id) {
			$conn = self::getConnection();
			$sql = sprintf(
				"select * from manual_alert where id = %d",
				$id
				);
			$rows = mysql_query($sql, $conn);
			$row = mysql_fetch_assoc($rows);
			return new ManualAlert(
					$row["alerter_id"],
					$row["type"],
					$row["sector"],
					$row["coords"],
					$row["header"],
					$row["detail"],
					$row["date"]
				); 
		}
		
		public function addSighting($sighting) {
			$conn = self::getConnection();
			$sql = sprintf(
				"insert into sighting (sighter_id, date, sector, coords, pilot_name) values (%d, utc_timestamp(), '%s', '%s', '%s')",
				$sighting->getSighterID(),
				$sighting->getSector(),
				$sighting->getCoords(),
				$sighting->getPilotName()
				);
			//echo $sql;	
			mysql_query($sql, $conn);
		}
		
		public function addManualAlert($manualAlert) {
			$conn = self::getConnection();
			$sql = sprintf(
				"insert into manual_alert (type, alerter_id, sector, coords, date, header, detail) values (%d, %d, '%s', '%s', utc_timestamp(), '%s', '%s')", 
				$manualAlert->getAlertType(),
				$manualAlert->getAlerterID(), 
				mysql_real_escape_string($manualAlert->getSector()),
				mysql_real_escape_string("[".$manualAlert->getCoords()."]"),
				mysql_real_escape_string($manualAlert->getHeader()),
				mysql_real_escape_string($manualAlert->getDetail())
				);
			//echo $sql;	
			mysql_query($sql, $conn);
		}
		
		public function getManualAlertTypeById($id) {
			$conn = self::getConnection();
			$sql = "select alert_type from manual_alert_type where id = ".$id;
			$rows = mysql_query($sql, $conn);
			$row = mysql_fetch_assoc($rows);
			if (!row)
				return null;
			else
				return $row["alert_type"];
		}
		
		public function getManualAlertTypes() {
			$alertTypes = array();
			$conn = self::getConnection();
			$sql = "SELECT * FROM manual_alert_type";
			$rows = mysql_query($sql, $conn);
			while ($row = mysql_fetch_assoc($rows)) {
				$alertType =
					new ManualAlertType(
						$row["id"],
						$row["alert_type"]
					);
				$alertTypes[] = $alertType;
			}
			return $alertTypes;
		}
		
		public function getSectors() {
			$sectors = array();
			$conn = self::getConnection();
			$sql = "SELECT * FROM sector order by sector";
			$rows = mysql_query($sql, $conn);
			while ($row = mysql_fetch_assoc($rows)) {
				$sectors[] = $row["sector"];
			}
			return $sectors;
		}

	}
?>